package classesInterface;

import classesBancodeDados.FichaSistematizacaoAssistEnf;
import classesBancodeDados.Paciente;
import classesBancodeDados.Pessoa;
import classesJpa.FichaSistematizacaoAssistEnfJpaController;
import classesJpa.exceptions.NonexistentEntityException;
import classesMiolo.Formatador;
import classesMiolo.SistAssistEnf;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

public class TelaFichaSistmAssistEnf extends javax.swing.JFrame {

    /**
     * Creates new form Sistm_Assist_Enf
     */
    private TelaFichaSistmAssistEnf() {
        initComponents();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    public static TelaFichaSistmAssistEnf getInstance(){
        if(instance == null) instance = new TelaFichaSistmAssistEnf();
        return instance;
    }
    
    public void limpaCampos(){
        TelaFichaSistmAssistEnf sae = TelaFichaSistmAssistEnf.getInstance();
        sae.campoEnferm.setText("");
        sae.campoLeito.setText("");
        sae.campoNome.setText("");
        sae.campoEnfermeiro.setText("");
        sae.campoData.setText("");
        sae.campoCoren.setText("");
        DefaultTableModel d1 = (DefaultTableModel) sae.jTable1.getModel();
        while(sae.jTable1.getRowCount() > 0){
            d1.removeRow(0);
        }
    }
    
    public void turnEditable(boolean editable){
        botaoSalvar.setEnabled(editable);
        TelaFichaSistmAssistEnf sae = TelaFichaSistmAssistEnf.getInstance();
        sae.campoEnferm.setEditable(false);
        sae.campoLeito.setEditable(false);
        sae.campoNome.setEditable(false);
        sae.campoCoren.setEditable(false);
        sae.campoEnfermeiro.setEditable(false);
        sae.campoData.setEditable(editable);
        sae.jTable1.setEnabled(editable);
    }
    
    public void preencherCampos(Paciente p, Pessoa e,  FichaSistematizacaoAssistEnf sae){
        this.campoEnferm.setText(sae.getEnfermaria());
        this.campoCoren.setText(sae.getCoren());
        this.campoData.setText(sae.getDataFicha());
        this.campoEnfermeiro.setText(e.getNome());
        this.campoLeito.setText("" + sae.getLeito());
        this.campoNome.setText(p.getNome());
        DefaultTableModel d1 = (DefaultTableModel)this.jTable1.getModel();
        for(SistAssistEnf i : sae.getSistematizacao()){
            String aux[] = new String[3];
            aux[0] = i.getDiagnostico();
            aux[1] = i.getIntervencao();
            aux[2] = i.getHorario();
            d1.addRow(aux);
        }
   }

    private void salvar(){
        ficha.setDataFicha(this.campoData.getText());
        ArrayList<SistAssistEnf> list = new ArrayList<>();
        DefaultTableModel d1 = (DefaultTableModel) this.jTable1.getModel();
        for(int i = 0; i < this.jTable1.getRowCount(); ++i){
            String[] aux = new String[3];
            for(int j = 0; j < this.jTable1.getColumnCount(); ++j){
                aux[j] = (String) d1.getValueAt(i, j);
            }
            SistAssistEnf sae = new SistAssistEnf(aux);
            list.add(sae);
        }
        ficha.setSistematizacao(list);
        FichaSistematizacaoAssistEnfJpaController saec = new FichaSistematizacaoAssistEnfJpaController();
        try {
            saec.edit(ficha);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(TelaFichaSistmAssistEnf.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TelaFichaSistmAssistEnf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField10 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        campoEnferm = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        campoLeito = new javax.swing.JTextField();
        botaoSalvar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        campoEnfermeiro = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        campoCoren = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        campoData = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        jTextField10.setText("diag3");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("SISTEMATIZAÇÃO DA ASSISTÊNCIA DE ENFERMAGEM");

        jLabel2.setText("Nome do Paciente: ");

        jLabel3.setText("Enfermaria: ");

        jLabel4.setText("Leito: ");

        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Diagnósticos de Enfermagem", "Intervenções de Enfermagem", "Horário"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel5.setText("Enfermeiro(a): ");

        campoEnfermeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoEnfermeiroActionPerformed(evt);
            }
        });

        jLabel6.setText("COREN: ");

        jLabel7.setText("Data:");

        campoData.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoDataKeyPressed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(campoEnferm, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(22, 22, 22)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(campoLeito, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(98, 98, 98))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(248, 248, 248))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoCoren, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoData, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botaoSalvar))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 516, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoEnfermeiro, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(campoEnferm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(campoLeito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(campoEnfermeiro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoSalvar)
                    .addComponent(jLabel6)
                    .addComponent(campoCoren, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(campoData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void campoEnfermeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoEnfermeiroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoEnfermeiroActionPerformed

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
        salvar();
        this.setVisible(false);
    }//GEN-LAST:event_botaoSalvarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        limpaCampos();
        setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        limpaCampos();
    }//GEN-LAST:event_formWindowClosing

    private void campoDataKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoDataKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoData.setText(Formatador.formataData(campoData.getText()));
        }
    }//GEN-LAST:event_campoDataKeyPressed

    public FichaSistematizacaoAssistEnf getFicha() {
        return ficha;
    }

    public void setFicha(FichaSistematizacaoAssistEnf ficha) {
        this.ficha = ficha;
    }    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*/* Set the Nimbus look and feel
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaFichaSistmAssistEnf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaFichaSistmAssistEnf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaFichaSistmAssistEnf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaFichaSistmAssistEnf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
          
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TelaFichaSistmAssistEnf.getInstance().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JTextField campoCoren;
    private javax.swing.JTextField campoData;
    private javax.swing.JTextField campoEnferm;
    private javax.swing.JTextField campoEnfermeiro;
    private javax.swing.JTextField campoLeito;
    private javax.swing.JTextField campoNome;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField10;
    // End of variables declaration//GEN-END:variables
    private static TelaFichaSistmAssistEnf instance = null;
    private FichaSistematizacaoAssistEnf ficha;
}
